<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top:10%;height: 80%;">
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel">
          取消
        </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="vendorModal">
          <a-row class="form-row" :gutter="24">
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车牌号">
                <a-input placeholder="请输入车牌号" v-decorator.trim="[ 'carNumber', validatorRules.supplier]"/>
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="司机姓名">
                <a-input placeholder="请输入司机姓名" v-decorator.trim="[ 'driver' ]"/>
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
                <a-input placeholder="请输入司机联系电话" v-decorator.trim="[ 'mobile' ]"/>
              </a-form-item>
            </a-col>
            <a-col :lg="18" :md="18" :sm="24">
              <a-form-item :labelCol="{xs: { span: 24 },sm: { span: 3 }}"
                           :wrapperCol="{xs: { span: 24 },sm: { span: 20 }}" label="车辆图片">
                <j-image-upload v-model="fileList" bizPath="material" text="上传" isMultiple></j-image-upload>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import {addSupplier, editSupplier, checkSupplier, saveCar} from '@/api/api'
import {autoJumpNextInput} from "@/utils/util"
import {mixinDevice} from '@/utils/mixin'
import JImageUpload from "@comp/jeecg/JImageUpload.vue";
import axios from "axios";
import {mapGetters} from "vuex";

export default {
  name: "CarModal",
  components: {JImageUpload},
  mixins: [mixinDevice],
  data() {
    return {
      fileList: [],
      title: "操作",
      visible: false,
      model: {},
      identityFileList: [],
      bankNoFileList: [],
      isReadOnly: false,
      labelCol: {
        xs: {span: 24},
        sm: {span: 4},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 20},
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        supplier: {
          rules: [
            {required: true, message: '请输入名称!'},
            {min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur'},
          ]
        }
      },
    }
  },
  created() {
    console.log(this.userInfo().customerId)
  },
  methods: {
    ...mapGetters(["nickname", "loginName", "userInfo"]),
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.fileList = record.image ? record.image.split(',') : []
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'carNumber', 'driver', 'mobile'))
        autoJumpNextInput('vendorModal')
      });
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let formData = Object.assign(this.model, values);


          let data = {
            ...formData,
            state: 1,
            isEmpower: 1,
            deviceNumber: 0,
            id: formData.carId
          }


          axios.post('http://terp.yixuanchang.cn/merchant/v1/car/addOrUpdateErp', data).then((res) => {
            console.log(res.data)
            if (res.data.code != 200) {
              throw new Error(res.data.message)
            }
            return {...formData, carId: res.data.data}
          }).then((res) => {
            return saveCar({
              ...res
            })
          }).then((res) => {
            console.log(res)
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped>

</style>